/*
Copyright [2020] [https://www.xiaonuo.vip]

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：

1.请不要删除和修改根目录下的LICENSE文件。
2.请不要删除和修改Snowy源码头部的版权声明。
3.请保留源码和相关描述文件的项目出处，作者声明等。
4.分发源码时候，请注明软件出处 https://gitee.com/xiaonuobase/snowy
5.在修改包名，模块名称，项目代码等时，请注明软件出处 https://gitee.com/xiaonuobase/snowy
6.若您的项目无法满足以上几点，可申请商业授权，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.modular.databaseinfo.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import vip.xiaonuo.core.exception.ServiceException;
import vip.xiaonuo.core.factory.PageFactory;
import vip.xiaonuo.core.pojo.page.PageResult;
import vip.xiaonuo.core.util.PoiUtil;
import vip.xiaonuo.modular.databaseinfo.entity.DatabaseInfo;
import vip.xiaonuo.modular.databaseinfo.enums.DatabaseInfoExceptionEnum;
import vip.xiaonuo.modular.databaseinfo.mapper.DatabaseInfoMapper;
import vip.xiaonuo.modular.databaseinfo.param.DatabaseInfoParam;
import vip.xiaonuo.modular.databaseinfo.service.DatabaseInfoService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.modular.tableinfo.entity.TableInfo;

import java.util.List;

/**
 * 数据库信息表service接口实现类
 *
 * @author wfy
 * @date 2022-07-05 15:19:26
 */
@Service
public class DatabaseInfoServiceImpl extends ServiceImpl<DatabaseInfoMapper, DatabaseInfo> implements DatabaseInfoService {

    @Override
    public PageResult<DatabaseInfo> page(DatabaseInfoParam databaseInfoParam) {
        QueryWrapper<DatabaseInfo> queryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotNull(databaseInfoParam)) {

            // 根据组织架构id 查询
            if (ObjectUtil.isNotEmpty(databaseInfoParam.getOrgId())) {
                queryWrapper.lambda().eq(DatabaseInfo::getOrgId, databaseInfoParam.getOrgId());
            }
            // 根据数据库权限分级 查询
            if (ObjectUtil.isNotEmpty(databaseInfoParam.getDbPermissions())) {
                queryWrapper.lambda().eq(DatabaseInfo::getDbPermissions, databaseInfoParam.getDbPermissions());
            }
            // 根据数据库名称（英文名称） 查询
            if (ObjectUtil.isNotEmpty(databaseInfoParam.getDbName())) {
                queryWrapper.lambda().eq(DatabaseInfo::getDbName, databaseInfoParam.getDbName());
            }
            // 根据数据库类型（英文名称） 查询
            if (ObjectUtil.isNotEmpty(databaseInfoParam.getDbType())) {
                queryWrapper.lambda().eq(DatabaseInfo::getDbType, databaseInfoParam.getDbType());
            }
            // 根据jdbc的驱动类型 查询
            if (ObjectUtil.isNotEmpty(databaseInfoParam.getJdbcDriver())) {
                queryWrapper.lambda().eq(DatabaseInfo::getJdbcDriver, databaseInfoParam.getJdbcDriver());
            }
            // 根据jdbc的驱动版本号 查询
            if (ObjectUtil.isNotEmpty(databaseInfoParam.getJdbcDriverVersion())) {
                queryWrapper.lambda().eq(DatabaseInfo::getJdbcDriverVersion, databaseInfoParam.getJdbcDriverVersion());
            }
            // 根据数据库连接的账号 查询
            if (ObjectUtil.isNotEmpty(databaseInfoParam.getUserName())) {
                queryWrapper.lambda().eq(DatabaseInfo::getUserName, databaseInfoParam.getUserName());
            }
            // 根据数据库连接密码 查询
            if (ObjectUtil.isNotEmpty(databaseInfoParam.getPassword())) {
                queryWrapper.lambda().eq(DatabaseInfo::getPassword, databaseInfoParam.getPassword());
            }
            // 根据jdbc的url 查询
            if (ObjectUtil.isNotEmpty(databaseInfoParam.getJdbcUrl())) {
                queryWrapper.lambda().eq(DatabaseInfo::getJdbcUrl, databaseInfoParam.getJdbcUrl());
            }
            // 根据实时获取或者离线获取 查询
            if (ObjectUtil.isNotEmpty(databaseInfoParam.getOnline())) {
                queryWrapper.lambda().eq(DatabaseInfo::getOnline, databaseInfoParam.getOnline());
            }
            // 根据数据库结构变化数量 查询
            if (ObjectUtil.isNotEmpty(databaseInfoParam.getDbChange())) {
                queryWrapper.lambda().eq(DatabaseInfo::getDbChange, databaseInfoParam.getDbChange());
            }
            // 根据备注，摘要 查询
            if (ObjectUtil.isNotEmpty(databaseInfoParam.getRemarks())) {
                queryWrapper.lambda().eq(DatabaseInfo::getRemarks, databaseInfoParam.getRemarks());
            }
        }
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<DatabaseInfo> list(DatabaseInfoParam databaseInfoParam) {
        return this.list();
    }

    @Override
    public void add(DatabaseInfoParam databaseInfoParam) {
        DatabaseInfo databaseInfo = new DatabaseInfo();
        BeanUtil.copyProperties(databaseInfoParam, databaseInfo);
        this.save(databaseInfo);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<DatabaseInfoParam> databaseInfoParamList) {
        databaseInfoParamList.forEach(databaseInfoParam -> {
            this.removeById(databaseInfoParam.getId());
        });
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(DatabaseInfoParam databaseInfoParam) {
        DatabaseInfo databaseInfo = this.queryDatabaseInfo(databaseInfoParam);
        BeanUtil.copyProperties(databaseInfoParam, databaseInfo);
        this.updateById(databaseInfo);
    }

    @Override
    public DatabaseInfo detail(DatabaseInfoParam databaseInfoParam) {
        return this.queryDatabaseInfo(databaseInfoParam);
    }

    /**
     * 获取数据库信息表
     *
     * @author wfy
     * @date 2022-07-05 15:19:26
     */
    private DatabaseInfo queryDatabaseInfo(DatabaseInfoParam databaseInfoParam) {
        DatabaseInfo databaseInfo = this.getById(databaseInfoParam.getId());
        if (ObjectUtil.isNull(databaseInfo)) {
            throw new ServiceException(DatabaseInfoExceptionEnum.NOT_EXIST);
        }
        return databaseInfo;
    }

    @Override
    public void export(DatabaseInfoParam databaseInfoParam) {
        List<DatabaseInfo> list = this.list(databaseInfoParam);
        PoiUtil.exportExcelWithStream("SnowyDatabaseInfo.xls", DatabaseInfo.class, list);
    }

    @Override
    public List<TableInfo> queryNum(String tio) {
        return null;
    }

}
